package com.neusoft.neuiotms.dcom.converter.incident;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.common.model.Dictionary;
import com.neusoft.neuiotms.common.repository.DictionaryRepository;
import com.neusoft.neuiotms.dcom.dto.incident.IncidentRegisterDTO;
import com.neusoft.neuiotms.dcom.model.incident.IncidentRegister;
import com.neusoft.neuiotms.dcom.model.incident.IncidentRegisterAttachment;
import com.neusoft.neuiotms.dcom.repository.incident.IncidentRegisterRepository;

/**
 * @author WYS
 * @date 2020年8月18日 下午2:02:51
 */
@Component
public class IncidentRegisterConverter extends AbstractConverter<IncidentRegister, IncidentRegisterDTO> {

    @Autowired
    private IncidentRegisterRepository incidentRegisterRepository;
    @Autowired
    private DictionaryRepository dictionaryRepository;
    @Autowired
    private IncidentRegisterAttachmentConverter incidentRegisterAttachmentConverter;

    @Override
    public IncidentRegister toModel(final IncidentRegisterDTO dto) {
        IncidentRegister model;
        if (dto.isNew()) {
            model = new IncidentRegister();
            model.setCreateTime(new Date());
        } else {
            model = this.incidentRegisterRepository.getOne(dto.getId());
            model.setUpdateTime(new Date());
        }

        model.setTaskId(dto.getTaskId());

        model.setWorkNum(dto.getWorkOrderNum());
        model.setName(dto.getName());

        model.setApplyTime(dto.getApplyTime());
        model.setApplyType(dto.getApplyType());

        if ((dto.getAttachments() != null) && !dto.getAttachments().isEmpty()) {
            final List<IncidentRegisterAttachment> list =
                    this.incidentRegisterAttachmentConverter.toListModel(dto.getAttachments());
            list.forEach(t -> {
                t.setIncidentRegister(model);
            });
            model.setAttachments(list);
        }

        model.setCommunicationNumber(dto.getCommunicationNumber());
        model.setCoverageInfluence(dto.getCoverageInfluence());
        model.setDescription(dto.getDescription());

        model.setDispatchUserCode(dto.getDispatchUserCode());
        model.setDispatchUserName(dto.getDispatchUserName());

        model.setIncidentFrom(dto.getIncidentFrom());
        model.setObjCode(dto.getObjCode());
        model.setObjName(dto.getObjName());
        model.setObjId(dto.getObjId());
        model.setPriority(dto.getPriority());
        model.setRegisterUserCode(dto.getRegisterUserCode());
        model.setRegisterUserName(dto.getRegisterUserName());
        model.setUrgency(dto.getUrgency());
        model.setApplyUserCode(dto.getApplyUserCode());
        model.setApplyUserName(dto.getApplyUserName());

        model.setStatus(dto.getStatus());

        return model;
    }

    @Override
    public IncidentRegisterDTO toDTO(final IncidentRegister model, final boolean forListView) {
        final IncidentRegisterDTO dto = new IncidentRegisterDTO();
        dto.setId(model.getId());

        dto.setTaskId(model.getTaskId());

        dto.setWorkOrderNum(model.getWorkNum());

        dto.setName(model.getName());

        dto.setApplyTime(model.getApplyTime());

        dto.setApplyType(model.getApplyType());
        if (model.getApplyType() != null) {
            final Dictionary dic = this.dictionaryRepository.findByCode(model.getApplyType());
            dto.setApplyTypeName(dic.getText());
        }

        if (!forListView) {
            dto.setAttachments(this.incidentRegisterAttachmentConverter.toListDTO(model.getAttachments()));
        }

        dto.setCommunicationNumber(model.getCommunicationNumber());

        dto.setCoverageInfluence(model.getCoverageInfluence());
        if (model.getCoverageInfluence() != null) {
            final Dictionary dic = this.dictionaryRepository.findByCode(model.getCoverageInfluence());
            dto.setCoverageInfluenceName(dic.getText());
        }

        dto.setDescription(model.getDescription());

        dto.setIncidentFrom(model.getIncidentFrom());
        if (model.getIncidentFrom() != null) {
            final Dictionary dic = this.dictionaryRepository.findByCode(model.getIncidentFrom());
            if (dic != null) {
                dto.setIncidentFromName(dic.getText());
            }
        }

        dto.setObjCode(model.getObjCode());
        dto.setObjName(model.getObjName());
        dto.setObjId(model.getObjId());
        dto.setPriority(model.getPriority());
        if (model.getPriority() != null) {
            final Dictionary dic = this.dictionaryRepository.findByCode(model.getPriority());
            if (dic != null) {
                dto.setPriorityName(dic.getText());
            }
        }

        dto.setRegisterUserCode(model.getRegisterUserCode());
        dto.setRegisterUserName(model.getRegisterUserName());

        dto.setDispatchUserCode(model.getDispatchUserCode());
        dto.setDispatchUserName(model.getDispatchUserName());

        dto.setUrgency(model.getUrgency());
        if (model.getUrgency() != null) {
            final Dictionary dic = this.dictionaryRepository.findByCode(model.getUrgency());
            if (dic != null) {
                dto.setUrgencyName(dic.getText());
            }
        }

        dto.setStatus(model.getStatus());

        dto.setApplyUserCode(model.getApplyUserCode());
        dto.setApplyUserName(model.getApplyUserName());
        dto.setCreateTime(model.getCreateTime());
        dto.setUpdateTime(model.getUpdateTime());

        return dto;
    }

}
